package cn.ycl.system.biz.file.controller;

import cn.ycl.constans.Result;
import cn.ycl.system.biz.file.entity.FileInfo;
import cn.ycl.system.other.utils.FileUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author YuanChangLiang
 * @Date 2020/12/29 11:05
 */
@RestController
@RequestMapping("/file")
public class FileController {
//    @Resource
//    RemoteUserService remoteUserService;

    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");


    /**
     * 文件上传（在用）
     *
     * @param file 文件
     * @return 格式化
     * @throws IOException 文件
     */
    @PostMapping("/upload")
    public Result importData(@RequestParam("file") MultipartFile file) throws IOException {
        if (file == null) {
            return Result.fail("文件为空");
        }
        String format = sdf.format(new Date());
        String realPath = "E:/all_file/ycl_image" + format;
        String oldName = file.getOriginalFilename();
        assert oldName != null;
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        FileUtil.uploadFile(file.getBytes(), realPath, newName);
        String finalPath = realPath + "/" + newName;
        //将图片路径入库
//        User user = new User();
//        user.setAvatarImages(finalPath);
//        remoteUserService.updateUser();
        //
        FileInfo fileInfo = new FileInfo();

        fileInfo.setFileName(newName);
        fileInfo.setFilePath(finalPath);
        fileInfo.setOldName(oldName);
        return Result.ok(fileInfo);
    }


}
